package com.ayuku.retail.business.Supplier;

import java.util.List;
import java.util.Map;

import com.ayuku.commons.AyukuDB;
import com.ayuku.retail.persistence.model.Supplier;

public class SupplierImpl {

	public static List<Supplier> getList(String arg) throws Exception {
		List<Supplier> lst = AyukuDB.getPersist().readList(
				Supplier.class,
				"SELECT * FROM Supplier WHERE "
						+ "ucase(code) LIKE ucase(?) "
						+ "or ucase(name) LIKE ucase(?)",
				new Object[] { arg, arg });
		return lst;
	}

	public static List<Map<String, Object>> getMapList(String arg)
			throws Exception {
		List<Map<String, Object>> lst = AyukuDB.getPersist().readMapList(arg);
		return lst;
	}

	public static Supplier getById(String key) throws Exception {
		Supplier obj = (Supplier) AyukuDB.getPersist().readByPrimaryKey(
				Supplier.class, new Object[] { key });
		return obj;
	}

	public static int add(Supplier obj) throws Exception {
		return AyukuDB.getPersist().insert(obj);
	}

	public static int update(Supplier obj) throws Exception {
		return AyukuDB.getPersist().update(obj);
	}

	public static int delete(String key) throws Exception {
		int n = AyukuDB.getPersist().executeUpdate(
				"delete from Supplier where trim(code)=?",
				new Object[] { key.trim() });
		return n;
	}

	public static int delete(List<String> ids) throws Exception {
		int n = 0;
		try {
			AyukuDB.getPersist().setAutoCommit(false);
			for (int j = 0; j < ids.size(); j++) {
				String key = ids.get(j);
				n += delete(key);
			}
			AyukuDB.getPersist().commit();
		} catch (Exception e) {
			e.printStackTrace();
			AyukuDB.getPersist().rollback();
			throw new Exception();
		} finally {
			AyukuDB.getPersist().setAutoCommit(true);
		}
		return n;
	}

}
